﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Framework.MVC.Validation
{
    public class UniValidate : IUniValidate
    {
        /// <summary> event,服务端验证及产生客户端验证js代码</summary>
        public Func<HttpRequestBase, bool> ValidE;
        private string _inputName;
        public bool isNew = true;
        public bool checked1 = true;
        public Dictionary<string, string[]> jsList = new Dictionary<string, string[]>();
        public string errStr;
        public UniValidate(string inputName)
        {
            _inputName = inputName;
        }
        /// <summary>增加JSON叶子节点</summary>
        /// <param name="type">验证类型</param>
        /// <param name="typeValue">类型值</param>
        /// <param name="message">错误提示消息</param>
        private void AddJs(string type, string typeValue, string message)
        {
            jsList.Add(type, new string[] { typeValue, message });
        }
        /// <summary>添加服务端的错误信息</summary>
        private void AddErrStr(string message)
        {
            errStr += message;
            checked1 = checked1 && false;
        }
        /// <summary>最后必需调用</summary>
        public MvcHtmlString ToString()
        {
            //return new MvcHtmlString("<p id='mes" + _inputName  + "' class='validMes' style='display:none'></p>");
            return new MvcHtmlString("");
        }
        /// <summary>加两边双引号</summary>
        public string AddMarks(string message)
        {
            return "\"" + message + "\"";
        }
        #region 所有的验证方法
        /// <summary>不能为空</summary>
        public IUniValidate NotEmpty(string message)
        {
            if (isNew)
            {
                AddJs("required", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] == null || request[_inputName].Length == 0) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>是否为Email格式</summary>
        public IUniValidate EmailAddress(string message)
        {
            if (isNew)
            {
                AddJs("email", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsEmail()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>正则验证</summary>
        public IUniValidate Regex(string regex, string message)
        {
            if (isNew)
            {
                AddJs("regex", AddMarks(regex), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsMatchRegex(regex)) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>中文</summary>
        public IUniValidate AllChinese(string message)
        {
            if (isNew)
            {
                AddJs("allChinese", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsAllChinese()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>与另一个控件值是否相等</summary>
        public IUniValidate EqualTo(string equalElement, string message)
        {
            if (isNew)
            {
                AddJs("equalTo", AddMarks("#" + equalElement), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName] != request[equalElement]) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>不能与另一个控件相等</summary>
        public IUniValidate NotEqualTo(string equalElement, string message)
        {
            if (isNew)
            {
                AddJs("notequalTo", AddMarks("#" + equalElement), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName] == request[equalElement]) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>不能小于最小值</summary>
        public IUniValidate Min(int num, string message)
        {
            if (isNew)
            {
                AddJs("min", num.ToString(), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName].ToDouble() < num) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>不能大于最大值</summary>
        public IUniValidate Max(int num, string message)
        {
            if (isNew)
            {
                AddJs("max", num.ToString(), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName].ToDouble() > num) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>长度不能小于</summary>
        public IUniValidate MinLength(int num, string message)
        {
            if (isNew)
            {
                AddJs("minlength", num.ToString(), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName].Length < num) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>长度不能大于</summary>
        public IUniValidate MaxLength(int num, string message)
        {
            if (isNew)
            {
                AddJs("maxlength", num.ToString(), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName].Length > num) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>验证长度在指定的区间内</summary>
        public IUniValidate Length(int min, int max, string message)
        {
            if (isNew)
            {
                AddJs("rangelength", string.Format("[{0},{1}]", min, max), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && (request[_inputName].Length > max || request[_inputName].Length < min)) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>身份证号码验证</summary>
        public IUniValidate IsIDCardNo(string message)
        {
            if (isNew)
            {
                AddJs("isIDCardNo", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsIDCard()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>远程验证 使用post方式 根据远程返回的Boolen值进行验证 true:表示可以提交，不显示错误提示 false:表示不可以提交，显示错误提示</summary>
        public IUniValidate Ajax(string url, string message)
        {
            if (isNew)
            {
                AddJs("remote", "{url:" + AddMarks(url) + ", type:\"post\"}", message);
            }
            return this;
        }
        /// <summary>日期验证</summary>
        public IUniValidate Date(string message)
        {
            if (isNew)
            {
                AddJs("date", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsDateTime()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>网址验证</summary>
        public IUniValidate Url(string message)
        {
            if (isNew)
            {
                AddJs("url", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsIDCard()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>Decimal 型数字验证</summary>
        public IUniValidate Decimal(string message)
        {
            if (isNew)
            {
                AddJs("digits", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsDecimal()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>检测是否为IP地址</summary>
        public IUniValidate IsIP(string message)
        {
            if (isNew)
            {
                AddJs("isIP", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsIP()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>不包含Html代码</summary>
        public IUniValidate NoHtml(string message)
        {
            if (isNew)
            {
                AddJs("noHtml", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName].IsHasHtml()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>不能包含中文或全角字符验证</summary>
        public IUniValidate NoChinese(string message)
        {
            if (isNew)
            {
                AddJs("noChinese", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName].IsHasChinese()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>判断是否是标准字符 如果包含非标准字符 报错</summary>
        public IUniValidate IsLegalXmlString(string message)
        {
            if (isNew)
            {
                AddJs("IsLegalXmlString", "true", message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && !request[_inputName].IsLegalXmlChar()) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>非 正则表达式，即不匹配此正则</summary>
        public IUniValidate NotRegex(string regex, string message)
        {
            if (isNew)
            {
                AddJs("notRegex", AddMarks(regex), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName].IsMatchRegex(regex)) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        /// <summary>Char最大长度</summary>
        public IUniValidate CharCodeLength(int num, string message)
        {
            if (isNew)
            {
                AddJs("charCodeLength", num.ToString(), message);
                ValidE += (HttpRequestBase request) =>
                {
                    if (request[_inputName] != null && request[_inputName].CharCodeLength() > num) AddErrStr(message);
                    return checked1;
                };
            }
            return this;
        }
        #endregion
    }
}
